/**
 * 机器管理路由
 *
 * Created by raquelmao on 2020/07/21.
 */
const middleware = require('./../middleware');
const PhoenixError = require('./../../models/phoenix-error');
const projectVersions = require('./../../models/project-versions');

/**
 * 获取默认编译配置选项
 * @param req
 * @param res
 * @param next
 */
function getDefaultProjectType(req, res, next) {
  const { projectId } = req.query;
  projectVersions.getDefaultProjectType(req.$token.userId, projectId)
    .then((projectType) => {
      res.json({ projectType });
    })
    .catch((err) => {
      next(new PhoenixError(err));
    });
}

/**
 * Export model definition object.
 */
module.exports = (app, prefix) => {
  const defaultProjectTypeURL = `${prefix}/machine/compilation-options/default-project-type.json`;

  // 获取默认编译器
  app.get(defaultProjectTypeURL, middleware.user.requireTokenUnexpired);
  app.get(defaultProjectTypeURL, middleware.user.requireAvailable);
  app.get(defaultProjectTypeURL, middleware.user.requireTester);
  app.get(defaultProjectTypeURL, getDefaultProjectType);
};
